from itsdangerous import TimedJSONWebSignatureSerializer,SignatureExpired
from h2003a_mdpro.settings import SECRET_KEY

user_token = TimedJSONWebSignatureSerializer(secret_key=SECRET_KEY,expires_in=86400)

def create_token(user_id):
    # 构造用户信息字典
    user_info = {'user_id':user_id}
    # 序列化用户信息
    token = user_token.dumps(user_info).decode()
    # 返回序列化以后的数据
    return token


def check_token(token):
    try:
        # 反序列化用户信息
        user_info = user_token.loads(token)
    # 如果同户信息超时，异常处理
    except SignatureExpired:
        # 处理完异常后，返回None
        return None
    else:
        # 返回用户字典
        return user_info

if __name__ == "__main__":
    token = create_token('1')

    print(check_token(token))